package com.retail.order.service.impl;

import com.retail.order.domain.TOrder;
import com.retail.order.service.ITOrderService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述:
 *
 * @version: java version 1.8
 * @author: liguangyao
 * @date: 2024-03-09 下午 8:04
 */
@Service
public class OrderBatchService {

    private static Integer MAX_TASK_NUM = 100;

    @Autowired
    private ITOrderService itOrderService;

    @Data
    public class OrderRequest{
        String requestId;//生成请求UUID
        Integer userId;//用户编号
        CompletableFuture<List<TOrder>> completableFuture;//获得我们需要的列表
    }

    private final Queue<OrderRequest> queue = new LinkedBlockingQueue<>();

    //定义初始化
    @PostConstruct
    public void init(){

        //定义一个线程时
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool (1);

        //调用线程用来实现定时执行
        scheduledExecutorService.scheduleAtFixedRate (()->{
            int size = queue.size ();
            if(size==0){
                return;
            }
            List<OrderRequest> list = new ArrayList<> ();
            //这里进行提示 合并了多少请求
            System.out.println ("===================合并了["+size+"]条数据=====================");
            for (int i = 0; i < size; i++) {
                if(i<MAX_TASK_NUM){
                    list.add (queue.poll ());//poll是取出首个元素并删除
                }
            }

            List<OrderRequest> orderReqs = new ArrayList<> ();
            for (OrderRequest orderRequest : list) {
                orderReqs.add (orderRequest);
            }
            //去查询队列中的信息
            Map<Integer,List<TOrder>> orderMap = itOrderService.selectTOrderMergeOrderList (orderReqs);
            
            //将各自的查询返回到各自的请求中
            for (OrderRequest request : list) {
                List<TOrder> orderList = orderMap.get (request.userId);
                request.completableFuture.complete (orderList);
                System.out.println (orderList);
            }
        },100,10, TimeUnit.MILLISECONDS);
        //MILLISECONDS 毫秒


    }

    public List<TOrder> getOrderListFromSomewhere(Integer userId) {
        OrderRequest orderRequest = new OrderRequest ();
        orderRequest.requestId= UUID.randomUUID ().toString ().replaceAll ("-","");
        orderRequest.userId=userId;
        CompletableFuture<List<TOrder>> future = new CompletableFuture<> ();
        orderRequest.completableFuture=future;
        queue.offer (orderRequest);
        try {
            return future.get ();
        } catch (InterruptedException e) {
            e.getStackTrace ();
        } catch (ExecutionException e) {
            e.getStackTrace ();
        }
        return null;
    }
}
